package org.tloss.web.editor.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.tloss.web.editor.model.Form;
import org.tloss.web.editor.model.FormField;
import org.tloss.web.editor.model.Message;
import org.tloss.web.editor.model.variable.EntityCommandVariable;

public class FactoryVariable {
	public void initParent(Variable parent, Variable child) {
		if (parent != null) {
			if (parent.getChildren() == null) {
				parent.setChildren(new ArrayList<Variable>());
			}
			parent.getChildren().add(child);
		}
		child.setParent(parent);
	}

	@SuppressWarnings("unchecked")
	public List<Variable> createVariable(Object obj) {
		if (obj != null) {
			if (obj instanceof EntityCommandVariable) {
				EntityVariable variable = new EntityVariable(((EntityCommandVariable) obj));
				List<Variable> rs = new ArrayList<Variable>();
				rs.add(variable);
				return rs;
			} else if (obj instanceof Message) {
				MessageVairable vairable = new MessageVairable();
				List<Variable> rs = new ArrayList<Variable>();
				rs.add(vairable);
				return rs;
			} else if (obj instanceof Form) {
				List<FormField> formFields = ((Form) obj).getFormFields();
				if (formFields != null) {
					List<Variable> rs = new ArrayList<Variable>();
					for (int i = 0; i < formFields.size(); i++) {
						FormFieldVariable fieldVariable = new FormFieldVariable();
						fieldVariable.setFormField(formFields.get(i));
						rs.add(fieldVariable);
					}
					return rs;
				}
			}
		}
		return Collections.EMPTY_LIST;
	}
}
